'use strict';

layui.define(['jquery','laytpl','laypage'], function(exports){
    let $ = layui.jquery
        ,laytpl = layui.laytpl
        ,laypage = layui.laypage;

    let obj = {
        //页码
        pageElem : null,
        count: 0,
        limit: 1,

        //容器
        box : null,
        url: '',
        tpl : '',

        //缓存
        cache: {},
        fields: {},

        //渲染完成后回调
        callback:null,

        init: function(obj, callback){
            this.pageElem = obj.pageElem;
            this.count = obj.count;
            this.limit = obj.limit;

            this.box = obj.box;
            this.url = obj.url;
            this.tpl = obj.tpl;
            this.callback = callback;


            //渲染页码
            this.renderPage();
            return this;
        },
        //渲染页码
        renderPage: function(skip){
            let that = this;
                skip = skip || false;
            laypage.render({
                elem: this.pageElem
                ,count: this.count
                ,limit : this.limit
                ,theme: '#1E9FFF'
                ,prev : '<i class="layui-icon layui-icon-left"></i>'
                ,next : '<i class="layui-icon layui-icon-right"></i>'
                ,jump : function(obj, first){
                    that.fields.curr = obj.curr;
                    if(!first || !skip)
                        that.refresh(obj);

                }
            });
        },
        //刷新数据
        refresh: function(obj){
            let loading = '<li style="width:100%;" class="text-center"><img src="/static/index/img/loading.gif" alt="Loading"></li>'
                ,box = $(this.box)
                ,that = this
                ,curr = this.fields.curr || 1
                ,limit = this.fields.limit || this.limit;

            obj = obj || {curr:curr,limit:limit};
            //Loading
            box.html(loading);


            //获取数据
            let data = that.cache[obj.curr];
            if (!data){

                $.extend(that.fields, {page:obj.curr, limit:obj.limit});/**/
                //ajax 请求数据
                $.ajax({
                    url: that.url,
                    type: "POST",
                    data: that.fields,
                    success: function(d) {
                        //如果请求失败
                        if (d.code !== 0){
                            console.log(d.msg);
                            return;
                        }
                        //设置缓存
                        data = that.cache[obj.curr] = d.data;
                        //渲染数据
                        that.renderPtl(data);
                    }
                });

            }else{
                //渲染数据
                that.renderPtl(data);
            }


        },
        renderPtl: function (d) {
            let that = this;
            //如果limit count改变 1.刷新页码
            if (d.total !== that.count || d.per_page !== that.limit){
                that.count = d.total;
                that.limit = d.per_page;
                //渲染页码
                that.renderPage(true);
            }

            //渲染模板
            laytpl(this.tpl).render(d.data, function(html){

                $(that.box).html(html);
                //回调
                if((typeof that.callback) === 'function'){
                    that.callback();
                }
            });
        },

        //改变url 删除缓存
        setFields: function(fields){
            if (JSON.stringify(this.fields) !== JSON.stringify(fields)) {
                this.fields = fields;
                this.cache = {};
            }
            return this;
        }
    };


    //输出test接口
    exports('posts', obj);
});